//
//  HomeEssentialViewController.m
//  AmountBody
//
//  Created by smm on 2017/12/14.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "HomeEssentialViewController.h"
#import "EssentialModel.h"
#import "RulerShowView.h"
#import "HomeAutonomyEssentialViewController.h"
#import "XLBubbleTransition.h"
#import "HomeDetailViewController.h"
#import "CustomNewsView.h"

static const CGFloat Text_Tag = 100000;
static const CGFloat View_Tag = 90000;

@interface HomeEssentialViewController ()<UITextFieldDelegate>
{
    UILabel * heightLabel;
    UILabel * weightLabel;
}
@property (weak, nonatomic) IBOutlet UIImageView *backImage;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) UIView * contentView;//scrollView的content
@property (strong, nonatomic) CustomNewsView * customView;//客户信息View
@property (weak, nonatomic) IBOutlet UIButton *xuanfuButton;
- (IBAction)xuanfuButton:(id)sender;

@end

@implementation HomeEssentialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.public_headView.hidden = NO;
    self.public_headView.leftButton = [self leftButton];
    self.public_headView.titleLabel = [self titleLabel];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadUI];
    
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = NO;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}

#pragma mark -  UI布局
-(void)leftButtonPressed
{
    NSArray * array = self.navigationController.viewControllers;
    CGFloat buttonW = 55;
    CGFloat buttonH = buttonW;
    CGRect rect = CGRectMake(SCREEN_W-buttonW, SCREEN_H-64-buttonH, buttonW, buttonH);
    HomeDetailViewController* controller = array[array.count - 2];
    controller.xl_pushTranstion = [XLBubbleTransition transitionWithAnchorRect:rect];
    controller.xl_popTranstion = [XLBubbleTransition transitionWithAnchorRect:rect];
    [self.navigationController popToViewController:controller animated:YES];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    CGFloat jianGeW = 25;
    CGFloat jianGeH = 15;
    CGFloat imageW = (SCREEN_W-jianGeW*2);
    CGFloat imageH = imageW/974*659;
    CGFloat h_imageW = 500;
    CGFloat h_imageH = 557;
    CGFloat top = 100; // 顶端盖高度
    CGFloat bottom = 20 ; // 底端盖高度
    CGFloat left = 80; // 左端盖宽度
    CGFloat right = 20; // 右端盖宽度
    [_headImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        layoutViews(^{ //横屏
            make.left.equalTo(_backImage).offset(-25);
            make.bottom.equalTo(_backImage).offset(20);
            make.width.equalTo(@(h_imageW));
            make.height.equalTo(@(h_imageH));
        }, ^{ //竖屏
            make.left.equalTo(_backImage).offset(-25);
            make.bottom.equalTo(_backImage).offset(40);
            make.width.equalTo(@(h_imageW));
            make.height.equalTo(@(h_imageH));
        });
    }];
    jianGeH = 80;
    
    [_scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        layoutViews(^{ //横屏
            make.left.equalTo(_headImage.mas_right);
            make.top.equalTo(_backImage).offset(jianGeH);
            make.right.equalTo(_backImage).offset(-right);
            make.bottom.equalTo(_backImage).offset(-bottom);
            make.height.equalTo(_backImage).offset(-bottom-jianGeH);
        }, ^{ //竖屏
            make.left.equalTo(_backImage).offset(left);
            make.top.equalTo(_backImage).offset(jianGeH);
            make.right.equalTo(_backImage).offset(-right);
            make.bottom.equalTo(_headImage.mas_top);
            make.width.equalTo(_backImage).offset(-left-right);
        });
    }];
    
    [_contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_scrollView);
        make.height.equalTo(_scrollView);
        make.width.equalTo(_scrollView);
    }];
    [_customView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(_contentView);
        layoutViews(^{ //横屏
            [_customView loadUIForCustomNewsViewHorizontal];
        }, ^{ //竖屏
            [_customView loadUIForCustomNewsViewVertical];
        });
    }];
    
    CGFloat viewW = 70;
    CGFloat viewH = viewW;
    //    _xuanfuButton.frame = CGRectMake(SCREEN_W-viewW-60, SCREEN_H-viewH-60, viewW, viewH);
    [_xuanfuButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        layoutViews(^{ //横屏
            make.right.equalTo(@-60);
            make.bottom.equalTo(@-60);
            make.width.equalTo(@(viewW));
            make.height.equalTo(@(viewH));
        }, ^{ //竖屏
            make.right.equalTo(@-60);
            make.bottom.equalTo(@-100);
            make.width.equalTo(@(viewW));
            make.height.equalTo(@(viewH));
        });
    }];
     [_xuanfuButton setYuanJiaoForRadius:viewH/2.0 andMasks:NO];
}
-(void)loadUI
{
    CGFloat jianGeW = 25;
    CGFloat jianGeH = 15;
    CGFloat imageW = (SCREEN_W-jianGeW*2);
    CGFloat imageH = imageW/974*659;
    CGFloat top = 100; // 顶端盖高度
    CGFloat bottom = 20 ; // 底端盖高度
    CGFloat left = 80; // 左端盖宽度
    CGFloat right = 20; // 右端盖宽度
    UIImage * image = [UIImage imageNamed:@"custom_custom_info"];
    
    // 指定为拉伸模式，伸缩后重新赋值
    image = [image stretchImageWithInsetsTop:top left:left bottom:bottom right:right];
    _backImage.image = image;
    _backImage.frame = CGRectMake(jianGeW, jianGeH+TOPNAVIGATIONBAR_H, imageW, imageH);
    [_backImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(jianGeW));
        make.top.equalTo(@(jianGeH+TOPNAVIGATIONBAR_H));
        make.right.equalTo(self.view).offset(-right);
        make.bottom.equalTo(self.view).offset(-40);
    }];
    [_backImage addSubview:_headImage];
    [_backImage addSubview:_scrollView];
    _headImage.image = [UIImage imageNamed:@"renwu"];
    _backImage.userInteractionEnabled = YES;
    _contentView = [[UIView alloc]init];
    [_contentView removeFromSuperview];
    [_scrollView addSubview:_contentView];
    [_customView removeFromSuperview];
    _customView = [[CustomNewsView alloc]init];
    [_contentView addSubview:_customView];
    UIInterfaceOrientation oritentation = [[UIApplication sharedApplication] statusBarOrientation];
    
    [_customView loadUIForCustomNewsView];
    _xuanfuButton.backgroundColor = RGBColorAlpha(66, 66, 66, 1);
    NSString * str = @"进入\n";
    NSString * str2 = @"自主选择";
    NSString * str3 = @"\n-标准号型-";
    NSString * needStr = [NSString stringWithFormat:@"%@%@%@",str,str2,str3];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:needStr];
    
    // 设置字体和设置字体的范围
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:12.0f]
                    range:NSMakeRange(0, needStr.length)];
    // 添加文字颜色
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor whiteColor]
                    range:NSMakeRange(0, needStr.length)];
    // 添加文字颜色
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:RGBColorAlpha(227, 65, 85, 1)
                    range:NSMakeRange(str.length, str2.length)];
    // 设置字体和设置字体的范围
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:15.0f]
                    range:NSMakeRange(str.length, str2.length)];
    [_xuanfuButton setAttributedTitle:attrStr forState:UIControlStateNormal];
    _xuanfuButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_xuanfuButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _xuanfuButton.titleLabel.font = [UIFont systemFontOfSize:12];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)xuanfuButton:(id)sender {
    self.xl_pushTranstion = [XLBubbleTransition transitionWithAnchorRect:_xuanfuButton.frame];
    self.xl_popTranstion = [XLBubbleTransition transitionWithAnchorRect:_xuanfuButton.frame];
    HomeAutonomyEssentialViewController * tmpVC = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"HomeAutonomyEssentialViewController"];
    tmpVC.title = @"基本信息";
    tmpVC.hidesBottomBarWhenPushed  = YES;
    [self.navigationController pushViewController:tmpVC animated:YES];
}
@end
